package com.sun.androidexerciseproject.function.http;

import com.sun.androidexerciseproject.function.util.LogUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpRequest {
    static final OkHttpClient mClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request()
                    .newBuilder()
                    .addHeader("User-Agent", "Android CHSP")
                    .addHeader("locale", "keep-alive")
                    .addHeader("Accept", "*/*")
                    .addHeader("dfpSessionId", "110005D001pagdBnI91XKne0GT87FR9pl3CrSusf7G")
                    .addHeader("cityCd", "130200")
                    .addHeader("urid", "127349696870749")
                    .addHeader("gray", "null")
                    .addHeader("key_session_id", "")
                    .addHeader("sid", "46300f7ad4c74a19977f1a9895d787df")
                    .build();
            return chain.proceed(request);
        }
    }).build();

    public static void getRequest(String url) {
        Request request = new Request.Builder().url(url).get().build();
        Call call = mClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LogUtil.log("onFailure reason = " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                LogUtil.log("onResponse result = " + result);
            }
        });
    }
}
